{{ define "main/version.html" }}
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    {{ template "views/header" . }}
</head>
<body class="animated fadeIn timo-layout-page">
<div class="page-loading">
    <div class="rubik-loader"></div>
</div>
<div class="animated fadeIn layui-card">
    <div class="layui-card-header timo-card-header">
        <span><i class="fa fa-bars"></i> 版本信息</span> <i class="layui-icon layui-icon-refresh refresh-btn"></i>
    </div>
    <div class="layui-card-body">
        {{ range .objectBaseBean }}
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">{{ .Group }}</label>
            <div class="layui-input-block">
                {{if ne .Key "android_version_control" }}<input type="text" id="{{ .Key }}" name="{{ .Key }}" lay-verify="required"
                       autocomplete="off" class="layui-input" value="{{ .Val }}">
                {{else}}<input type="hidden" id="{{ .Key }}" name="{{ .Key }}" lay-verify="required"
                               autocomplete="off" class="layui-input" value="{{ .Val }}">
                <div class="layui-btn-container tag" lay-filter="version-config" lay-allowclose="true" lay-newTag="true">
                {{$arr := split .Val "|"}}{{ range $k, $v := $arr }}<button lay-id="{{$v}}" type="button" class="tag-item">{{$v}}</button>
                {{ end }}</div>{{end}}
            </div>
        </div>{{ end }}
        <div class="layui-form-item">
            <label class="layui-form-label">上传文件</label>
            <div class="layui-input-inline uploadHeadImage">
                <div class="layui-upload-drag" id="uploadFile">
                    <i class="layui-icon"></i>
                    <p>点击上传文件，或将文件拖拽到此处！</p>
                </div>
            </div>
            <div class="layui-input-inline">
                <div id="uploadResult" class="layui-upload-list">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" id="commit" lay-submit lay-filter="upload">上传文件</button>
                <button lay-filter="commit" id="updateInfo" class="layui-btn layui-btn-normal">更新版本信息</button>
            </div>
        </div>
    </div>
</div>
<body>
{{ template "views/footer" . }}
<script>
    layui.config({
        base: '/assets/vendor/layui/modules/' //假设这是你存放拓展模块的根目录
    }).extend({ //设定模块别名
        tag: 'tag' //如果 tag.js 是在根目录，也可以不用设定别名
    }).use(['form', 'layer', 'upload', 'tag'], function () {
        var $ = layui.jquery, form = layui.form, layer = layui.layer;
        var tag = layui.tag;
        var upload = layui.upload;
        var uploadInst = upload.render({
            elem: '#uploadFile'
            , url: '/api/v1/file/upload'
            , size: 1024 * 50
            , accept: 'file'
            , multiple: true
            , auto: false
            , bindAction: '#commit'
            , before: function () {
                var index = parent.layer.msg('上传中..', {
                    icon: 16
                    , shade: 0.5
                    , time: false
                });
            }
            , done: function (res, index, upload) {
                parent.layer.close(index);
                if (res.code === 200) {
                    parent.layer.msg('上传成功', {
                        icon : 6
                    })
                    $('#uploadImg').hide();
                    $('#android_apk_url').val(res.data);
                } else
                    this.error(index, upload);

            }
            , error: function (index, upload) {
                layer.msg("上传失败")
            }
        });

        $("#updateInfo").on("click", function () {
            var str = ''
            $('[lay-filter="version-config"]').children('.tag-item').each(function(){
                str += $(this).attr('lay-id').trim() + "|";
            });
            str = str.substring(0, str.lastIndexOf('|'));
            $('#android_version_control').attr("value", str)

            var json = ["android_apk_url", "android_app_version", "android_update_content", "android_update_time",
                "android_version_control"];
            var keyPairs = [];
            for (let i = 0; i < json.length; i++) {
                keyPairs[i] = {'key': json[i], 'value': $('#'+ json[i]).val()};
            }
            var jsonData = {"req": keyPairs}
            ajaxJsonRequest("POST", '/api/v1/version/post', JSON.stringify(jsonData), function (result) {
                if (result.code === 200) {
                    addDone(result.message)
                }
            });
            return false;
        });

        function addDone(data) {
            //parent.layer.close(index);
            parent.layer.msg("更新成功", {
                icon: 6
            });
        }
    });
</script>
</body>
</html>
{{ end }}